"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SocketClientMsgHandler = void 0;
const RetCodeDefine_1 = require("../../../config/RetCodeDefine");
const UserModel_1 = require("../../../model/UserModel");
const DateUtil_1 = require("../../utils/DateUtil");
const Logger_1 = require("../../utils/Logger");
const ProtoConfig_1 = require("../protocol/ProtoConfig");
const ProtoUtils_1 = require("../protocol/ProtoUtils");
const SocketConnectionManager_1 = require("./SocketConnectionManager");
const SocketMsg_1 = require("./SocketMsg");
const RetCodeModel_1 = require("../../../model/RetCodeModel");
const Md5Helper_1 = require("../../utils/Md5Helper");
class SocketClientMsgHandler {
    static onEventSocketGata(socketConnect, msg) {
        // Logger.log("SocketClientMsgHandler onmsg=", socketConnect, msg);
        try {
            if (msg.protoName == ProtoConfig_1.ProtoConfig.req_register) {
                this.on_req_register(socketConnect, msg);
            }
            else if (msg.protoName == ProtoConfig_1.ProtoConfig.req_login) {
                this.on_req_login(socketConnect, msg);
            }
            else {
                if (socketConnect.isLogin()) {
                    if (msg.protoName == ProtoConfig_1.ProtoConfig.req_register) {
                    }
                    else {
                        Logger_1.Logger.error("不识别的Proto", msg.protoName);
                    }
                }
                else {
                    Logger_1.Logger.error("未登陆就发信息", msg.protoName);
                    this.sendErrorToClient(socketConnect, RetCodeDefine_1.RetCodeDefine.LoginError_NoLogin);
                }
            }
        }
        catch (error) {
            Logger_1.Logger.error("SocketClientMsgHandler error==", error);
        }
    }
    //单纯把错误信息发送给客户端
    static sendErrorToClient(conn, retCode, reqId = 0) {
        let protoType = ProtoConfig_1.ProtoConfig.getProtoResponeseType(ProtoConfig_1.ProtoConfig.resp_error_code);
        let msg = RetCodeDefine_1.RetCodeDefine.code2Msg.get(retCode);
        Logger_1.Logger.log("sendErrorToClient====", conn, retCode, msg);
        let model = RetCodeModel_1.RetCodeModel.createRetCodeModel(retCode, msg);
        let retCodeProto = RetCodeModel_1.RetCodeModel.serializeBinary(model);
        let socketMsg = SocketMsg_1.SocketMsg.createSocketMsg(reqId, protoType, ProtoConfig_1.ProtoConfig.resp_error_code, retCodeProto);
        conn.sendMsgToClient(socketMsg);
    }
    static on_req_register(socketConnect, msg) {
        return __awaiter(this, void 0, void 0, function* () {
            let reqPack = ProtoUtils_1.ProtoUtils.DeserializeBinary(ProtoConfig_1.ProtoConfig.req_register, msg.protoData);
            let userName = reqPack.getUsername();
            let pass = reqPack.getPass();
            let ret = yield UserModel_1.UserModel.register(userName, pass);
            // Logger.log("on_req_register===", ret)
            let respPack = ProtoUtils_1.ProtoUtils.Create(ProtoConfig_1.ProtoConfig.resp_register);
            respPack.setRetcode(ret);
            socketConnect.createMsgToClient(msg.reqId, ProtoConfig_1.ProtoConfig.resp_register, respPack);
        });
    }
    static on_req_login(socketConnect, msg) {
        return __awaiter(this, void 0, void 0, function* () {
            let protoPack = ProtoUtils_1.ProtoUtils.DeserializeBinary(ProtoConfig_1.ProtoConfig.req_login, msg.protoData);
            let loginType = protoPack.getLogintype();
            let userName = protoPack.getUsername();
            let pass = protoPack.getPass();
            let sysInfoPack = protoPack.getSysinfo();
            let resultObj = yield UserModel_1.UserModel.login(userName, pass, sysInfoPack, loginType);
            let ret = resultObj["ret"];
            let conn = SocketConnectionManager_1.SocketConnectionManager.instance.getSocketConnectionByUser(userName, socketConnect);
            if (conn && conn.id != socketConnect.id) {
                this.sendErrorToClient(conn, RetCodeDefine_1.RetCodeDefine.LoginError_MultiLogin);
                conn.gotoClose();
            }
            let respPack = ProtoUtils_1.ProtoUtils.Create(ProtoConfig_1.ProtoConfig.resp_login);
            respPack.setRetcode(ret);
            if (ret == RetCodeDefine_1.RetCodeDefine.OK) {
                let user = resultObj["user"];
                user.lastLoginIp = socketConnect.ip;
                user.lastLoginTime = DateUtil_1.default.now();
                user.token = Md5Helper_1.Md5Helper.createToken(user.userName);
                user.toUsersRedis();
                socketConnect.user = user;
                respPack.setUserinfo(user.toProto());
            }
            Logger_1.Logger.log("onLogin===", protoPack, respPack);
            socketConnect.createMsgToClient(msg.reqId, ProtoConfig_1.ProtoConfig.resp_login, respPack);
        });
    }
    static sendZonInfoToAllClient() {
        // // let socketMsg: SocketMsg = ZoneModel.createZoneInfoSocketMsg();
        // for (let i = 0; i < SocketConnectionManager.instance.socketConnList.length; i++) {
        //     let conn: SocketConnection = SocketConnectionManager.instance.socketConnList[i];
        //     if (conn && conn.user && conn.user.roleType != UserModel.Role_Type.Server) {
        //         // conn.sendMsgToClient(socketMsg);
        //     }
        // }
    }
}
exports.SocketClientMsgHandler = SocketClientMsgHandler;
